package com.example.zhangxin.startproject_124zx;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;

import com.example.zhangxin.startproject_124zx.bean.WeatherInfo;
import com.example.zhangxin.startproject_124zx.iface.WeatherListener;
import com.example.zhangxin.startproject_124zx.model.WeatherModel;
import com.example.zhangxin.startproject_124zx.model.Weather51Model;

public class MainActivity extends AppCompatActivity implements WeatherListener,View.OnClickListener{
    private EditText etcity;
    private Button btnfindl,btnfind2;
    private TextView tvcityname;
    private TextView cityNO;
    private TextView temp;
    private TextView wd;
    private TextView ws;
    private TextView sd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();//调用初始化方法，实例化控件
    }

    private void init()
    {
        etcity=(EditText)findViewById(R.id.editText);
        btnfindl=(Button)findViewById(R.id.button);
        btnfind2=(Button)findViewById(R.id.button2);
        tvcityname=(TextView)findViewById(R.id.textView);
        btnfindl.setOnClickListener(this);
        btnfind2.setOnClickListener(this);
        cityNO = (TextView) findViewById(R.id.textView2);
        temp = (TextView) findViewById(R.id.textView3);
        wd = (TextView) findViewById(R.id.textView4);
        ws = (TextView) findViewById(R.id.textView5);
        sd = (TextView) findViewById(R.id.textView6);
    }

    @Override
    public void onResponse(WeatherInfo weatherInfo) {
        if (weatherInfo!=null)
        {
            tvcityname.setText(weatherInfo.getCity());
            //课下自行添加其他天气信息
            cityNO.setText(weatherInfo.getCityid());
            temp.setText(weatherInfo.getTemp());
            wd.setText(weatherInfo.getWD());
            ws.setText(weatherInfo.getWS());
            sd.setText(weatherInfo.getSD());
        }
        else
            tvcityname.setText("未知");
    }

    @Override
    public void onFail(String msg) {
        tvcityname.setText(msg);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.button://中国天气
                System.out.println("--按钮1被点击");
                WeatherModel weatherModel=new WeatherModel();
                weatherModel.getWeather(etcity.getText().toString(),this);
                break;
            case R.id.button2://51万年历天气
                System.out.println("--按钮2被点击");
                Weather51Model weather51Model = new Weather51Model();
                weather51Model.getWeather(etcity.getText().toString(), this);
                break;
            default:
                break;
        }

    }


}
